// 1.导入相关依赖包
const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')
const { v4: uuidv4 } = require('uuid')

// 2.定义数据类型
/**
 * query类型是默认客户端查询的类型
 * 该类型在服务端必须存在且唯一
 */

const typeDefs = gql`
  #学生类型
  type Student {
    id: ID
    sname: String
    age: Int
    gender: Boolean
    score: Float
  }
  #查询类型
  type Query {
    hello: String
    # 参数可以指定默认值
    stu(n: Int = 11): Student
  }
`

// 3.解析数据类型对应的具体数据
const resolvers = {
  Query: {
    hello: () => 'Hello World!',
    stu: (obj, args) => {
      // 这里提供学生相关的数据
      return {
        sname: '张三',
        age: 12,
        gender: true,
        score: 99.5,
        id:uuidv4()
      }
    }
  }
}

// 4.整合apollo-server和express
/**
 *   typeDefs,resolvers 属性名是固定的
 */
const server = new ApolloServer({
  typeDefs,
  resolvers
})

const app = express()
server.applyMiddleware({ app })

// 5.监听启动
app.listen({ port: 4000 }, () => console.log('Now browse to http://localhost:4000' + server.graphqlPath))
